package com.liwq.cloudstudyconsumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.nacos.common.utils.StringUtils;
import com.ctc.wstx.util.StringUtil;
import com.liwq.cloudstudyconsumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : liwq
 * @version : 1.0
 * @date : 2022/7/5
 **/
@RestController
@RequestMapping("/consumer")
public class HelloConsumer {

    @Autowired
    private HelloService service;

    @RequestMapping("/callProvider")
    @SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler",
            exceptionsToIgnore = {IllegalArgumentException.class})
    public String callProvider(String userName) {
        if (StringUtils.isBlank(userName)) {
            throw new IllegalArgumentException("异常");
        }
        return service.hi(userName
        );
    }


    public String handlerFallback() {
        return "网络故障";
    }

    public String blockHandler() {
        return "网络故障";
    }

}
